package gov.mogaha.ntis.cmm.mob.law.activity;

import gov.mogaha.ntis.cmm.mob.R;
import gov.mogaha.ntis.cmm.mob.baseactivity.BaseActivity;
import gov.mogaha.ntis.cmm.mob.law.util.CmmMobLawFileUtil;
import gov.mogaha.ntis.cmm.mob.law.util.CmmMobLawHttpUtil;
import gov.mogaha.ntis.cmm.mob.law.util.CmmMobLawStr;
import gov.mogaha.ntis.cmm.mob.law.util.CmmMobLawStrUtil;
import gov.mogaha.ntis.cmm.mob.law.view.CmmMobLawTitleView;
import gov.mogaha.ntis.cmm.mob.util.Logger;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Toast;


public class CmmMobLawContentActivity extends BaseActivity{
	
	WebView webView;
	Activity activity;
	
	CmmMobLawTitleView cmmMobLawTitleView;
	Button prevBtn;
	Button exitBtn;
	
	ProgressDialog dial;
	
	String url;
	String chgUrl;
	String filePath;
		

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		Context context = getApplicationContext();
		
		// ProgressBar
		/*
		getWindow().requestFeature(Window.FEATURE_PROGRESS);
		
		setContentView(R.layout.cmmmoblawcontentview);
		*/
		
		/* Title 2 Start*/
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.cmmmoblawcontentview);
        this.cmmMobLawTitleView = (CmmMobLawTitleView) findViewById(R.id.title_view);
		this.cmmMobLawTitleView.setTitle(CmmMobLawStr._TITLE_NAME, 16);
		this.prevBtn = this.cmmMobLawTitleView.openLeftBtn();
		this.prevBtn.setOnClickListener(new View.OnClickListener() {
			
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		this.cmmMobLawTitleView.openRightBtn();
		
		this.exitBtn = this.cmmMobLawTitleView.openRightBtn();
		this.exitBtn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// 종료
				CmmMobLawListActivity mainActivity = (CmmMobLawListActivity)CmmMobLawListActivity.mainActivity;
				mainActivity.finish();
				mDialogHandler.sendEmptyMessage(DIALOG_ID_CLOSE);
			}
		});
		/* Title 2 End*/
		
		dial = new ProgressDialog(CmmMobLawContentActivity.this);
        dial.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        dial.setMessage("Wait a monent...");
        dial.show();
        
        // filePath 얻기
        filePath = context.getFilesDir().getPath()+"/";
		
        activity = this;
		
		webView = (WebView)findViewById(R.id.webview);
		webView.setWebChromeClient(new ProxyWebChromeClient());
		new Thread(new Runnable() {
			
			public void run() {
				url = getIntent().getSerializableExtra("url").toString();
				
				//데이터상에서 불러오는 링크는 header,속도,화면문제로 인해 원문링크로 url수정
				//chgUrl = CmmMobLawStr._LAWURL+url.replace("/DRF/lawService.do?OC=etoile79&target=law&MST=", "/LSW/lsInfoP.do?chrClsCd=010204&lsiSeq=").replace("&type=HTML", "");
				chgUrl = CmmMobLawStr._LAWURL+url.replace("/DRF/lawService.do?OC=open&target=law&MST=", "/LSW/lsInfoR.do?lsiSeq=").replace("&type=HTML", "");
				
				webView.getSettings().setJavaScriptEnabled(true);
				webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
				//webView.getSettings().setUseWideViewPort(true);
				webView.getSettings().setAllowFileAccess(true);
				webView.getSettings().setLightTouchEnabled(true);
				webView.getSettings().setBuiltInZoomControls(true);
				webView.setHorizontalScrollBarEnabled(false);
				
				
				
				final Activity activity = CmmMobLawContentActivity.this;
				
				webView.setWebChromeClient(new WebChromeClient() {
					public void onProgressChanged(WebView view, int progress) {
						activity.setProgress(progress * 100);
						Logger.d("progress:" +progress);
						if(progress == 100){
							mHandler.sendEmptyMessage(0);
						}
					}
				});
				
				webView.setWebViewClient(new WebViewClient() {
					public void onReceivedError(WebView view, int errorCode, 	String description, String fallingUrl) {
						Toast.makeText(activity, "로딩오류" + description,Toast.LENGTH_SHORT).show();
					}
					
				});
				
				
				Logger.d("URL : " + chgUrl);
			
				//Log.d(CmmMobLawStr._DEBUG, "contentURL : " + HttpUtil.fromHtmlToStr(chgUrl));
				
				//HttpUtil.fromHtmlToStr("http://www.law.go.kr/LSW/lsInfoP.do?lsiSeq=102121&chrClsCd=010202&urlMode=lsInfoP#0000");
				//Log.d(CmmMobLawStr._DEBUG, "contentURL : " + HttpUtil.fromHtmlToStr("http://www.law.go.kr/LSW/lsInfoP.do?lsiSeq=102121&chrClsCd=010202&urlMode=lsInfoP#0000"));
				//Log.d(CmmMobLawStr._DEBUG, "Context.getFilesDir() : " + filePath);
				
				CmmMobLawFileUtil.chkFile(filePath, CmmMobLawStr._CSS_PATH, CmmMobLawStr._CSS_FILES, CmmMobLawContentActivity.this);
				CmmMobLawFileUtil.writeFile(filePath, CmmMobLawStr._HTML_FILE_NAME, CmmMobLawStrUtil.getHtmlStartWithCSS(CmmMobLawStr._CSS_FILES)+ CmmMobLawHttpUtil.fromHtmlToStr(chgUrl));
			
				webView.loadUrl("file:///" +filePath + CmmMobLawStr._HTML_FILE_NAME);
				//webView.loadData(, "text/html", "UTF-16");
				
			}
		}).start();
	}
	
	Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			switch(msg.what){
			case 0:
				dial.dismiss();
				break;
			default:
				break;
			}
		}
	};
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}
	
	
	public class ProxyWebChromeClient extends WebChromeClient {
	    public void onProgressChanged(WebView view, int newProgress) {
	    	// TODO Auto-generated method stub
	    }
	}
}
